home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 19 / CU Amiga Magazine's Super CD-ROM 19 (1998)(EMAP Images)(GB)[!][issue 1998-02].iso / CUCD / Programming / LEDA / prog / dict / dic_test.c < prev    next >
C/C++ Source or Header  |  1994-08-05  |  5KB  |  203 lines

  1.  
  2. #include <LEDA/dictionary.h>
  3.  
  4. #include <LEDA/impl/ch_hash.h>
  5. #include <LEDA/impl/ch_hash1.h>
  6. #include <LEDA/impl/dp_hash.h>
  7. #include <LEDA/impl/avl_tree.h>
  8. #include <LEDA/impl/bin_tree.h>
  9. #include <LEDA/impl/rs_tree.h>
  10. #include <LEDA/impl/rb_tree.h>
  11. #include <LEDA/impl/skiplist.h>
  12. #include <LEDA/impl/ab_tree.h>
  13. #include <LEDA/impl/bb_tree.h>
  14.  
  15.  
  16. #if !defined(__TEMPLATE_ARGS_AS_BASE__)
  17. declare3(_dictionary,int,int,ch_hash)
  18. declare3(_dictionary,int,int,ch_hash1)
  19. declare3(_dictionary,int,int,dp_hash)
  20. declare3(_dictionary,int,int,avl_tree)
  21. declare3(_dictionary,int,int,bin_tree)
  22. declare3(_dictionary,int,int,rb_tree)
  23. declare3(_dictionary,int,int,rs_tree)
  24. declare3(_dictionary,int,int,skiplist)
  25. declare3(_dictionary,int,int,ab_tree)
  26. declare3(_dictionary,int,int,bb_tree)
  27.  
  28. declare3(_dictionary,float,float,avl_tree)
  29. declare3(_dictionary,float,float,bin_tree)
  30. declare3(_dictionary,float,float,rb_tree)
  31. declare3(_dictionary,float,float,rs_tree)
  32. declare3(_dictionary,float,float,skiplist)
  33. declare3(_dictionary,float,float,ab_tree)
  34. declare3(_dictionary,float,float,bb_tree)
  35. #endif
  36.  
  37.  
  38. void dic_test(dictionary<int,int>& D, int N, int* A, char* name)
  39.   cout << string("%-12s",name);
  40.   cout.flush();
  41.  
  42.   float T;
  43.   float T0 = T = used_time();
  44.  
  45.   for(int i=0; i<N; i++)  D.insert(A[i],0);
  46.   cout << string("%10.2f",used_time(T));
  47.   cout.flush();
  48.  
  49.   for(i=0; i<N; i++)  
  50.   { dic_item it = D.lookup(A[i]);
  51.     if (it == nil || D.key(it) != A[i]) error_handler(1,"error in lookup");
  52.    }
  53.  
  54.   cout << string("%10.2f",used_time(T));
  55.   cout.flush();
  56.  
  57.   for(i=0; i<N; i++)  D.del(A[i]);
  58.   cout << string("%10.2f",used_time(T));
  59.  
  60.   cout << string("%10.2f",used_time(T0));
  61.  
  62.   if (!D.empty()) cout << " NOT EMPTY !!\n";    
  63.  
  64.   newline;
  65.  
  66.   memory_clear();
  67.  
  68. }
  69.  
  70.  
  71. void dic_test(dictionary<float,float>& D, int N, float* A, char* name)
  72.   cout << string("%-12s",name);
  73.   cout.flush();
  74.  
  75.   D.clear();
  76.  
  77.   float T;
  78.   float T0 = T = used_time();
  79.  
  80.  
  81.   for(int i=0; i<N; i++)  D.insert(A[i],0);
  82.   cout << string("%10.2f",used_time(T));
  83.   cout.flush();
  84.  
  85.   for(i=0; i<N; i++)  D.lookup(A[i]);
  86.   cout << string("%10.2f",used_time(T));
  87.   cout.flush();
  88.  
  89.   for(i=0; i<N; i++)  D.del(A[i]);
  90.   cout << string("%10.2f",used_time(T));
  91.  
  92.   cout << string("%10.2f",used_time(T0));
  93.   newline;
  94.  
  95.   memory_clear();
  96. }
  97.  
  98.  
  99.  
  100. main()
  101. {
  102.  
  103. #if defined(__TEMPLATE_ARGS_AS_BASE__)
  104.   _dictionary<int,int,ch_hash> CHH_DIC;
  105.   _dictionary<int,int,ch_hash1> CH1_DIC;
  106.   _dictionary<int,int,dp_hash> DPH_DIC;
  107.  
  108.   _dictionary<int,int,avl_tree> AVL_DIC;
  109.   _dictionary<int,int,bin_tree> BIN_DIC;
  110.   _dictionary<int,int,rb_tree>  RB_DIC;
  111.   _dictionary<int,int,rs_tree>  RS_DIC;
  112.   _dictionary<int,int,skiplist> SK_DIC;
  113.   _dictionary<int,int,bb_tree>  BB_DIC;
  114.   _dictionary<int,int,ab_tree>  AB_DIC;
  115.  
  116.   _dictionary<float,float,avl_tree> AVL_DIC1;
  117.   _dictionary<float,float,bin_tree> BIN_DIC1;
  118.   _dictionary<float,float,rb_tree>  RB_DIC1;
  119.   _dictionary<float,float,rs_tree>  RS_DIC1;
  120.   _dictionary<float,float,skiplist> SK_DIC1;
  121.   _dictionary<float,float,bb_tree>  BB_DIC1;
  122.   _dictionary<float,float,ab_tree>  AB_DIC1;
  123. #else
  124.   _dictionary(int,int,ch_hash) CHH_DIC;
  125.   _dictionary(int,int,ch_hash1) CH1_DIC;
  126.   _dictionary(int,int,dp_hash) DPH_DIC;
  127.  
  128.   _dictionary(int,int,avl_tree) AVL_DIC;
  129.   _dictionary(int,int,bin_tree) BIN_DIC;
  130.   _dictionary(int,int,rb_tree)  RB_DIC;
  131.   _dictionary(int,int,rs_tree)  RS_DIC;
  132.   _dictionary(int,int,skiplist) SK_DIC;
  133.   _dictionary(int,int,bb_tree)  BB_DIC;
  134.   _dictionary(int,int,ab_tree)  AB_DIC;
  135.  
  136.   _dictionary(float,float,avl_tree) AVL_DIC1;
  137.   _dictionary(float,float,bin_tree) BIN_DIC1;
  138.   _dictionary(float,float,rb_tree)  RB_DIC1;
  139.   _dictionary(float,float,rs_tree)  RS_DIC1;
  140.   _dictionary(float,float,skiplist) SK_DIC1;
  141.   _dictionary(float,float,bb_tree)  BB_DIC1;
  142.   _dictionary(float,float,ab_tree)  AB_DIC1;
  143. #endif
  144.  
  145.   int    N     = read_int("# keys = ");
  146.   int*   Int   = new int[N];
  147.   int*   Int1  = new int[N];
  148.   float* Float = new float[N];
  149.  
  150.   //init_random(N);
  151.  
  152.   for(int i=0; i<N; i++) Float[i] = Int[i] = random(0,10000000);
  153.  
  154.   for(i=0; i<N; i++) Int1[i] = i;
  155.  
  156.  
  157.   newline;
  158.   cout << "                insert    lookup    delete     total\n";
  159.   newline;
  160.  
  161.  
  162.  
  163.   dic_test(CHH_DIC,N,Int,"ch_hash");
  164.   dic_test(CH1_DIC,N,Int,"ch_hash1");
  165.   dic_test(DPH_DIC,N,Int,"dp_hash");
  166.   newline;
  167.   dic_test(SK_DIC,N,Int,"skiplist");
  168.   dic_test(RS_DIC,N,Int,"rs_tree");
  169.   dic_test(BIN_DIC,N,Int,"bin_tree");
  170.   dic_test(RB_DIC,N,Int,"rb_tree");
  171.   dic_test(AVL_DIC,N,Int,"avl_tree");
  172.   dic_test(BB_DIC,N,Int,"bb_tree");
  173.   dic_test(AB_DIC,N,Int,"ab_tree");
  174.   newline;
  175.  
  176.   dic_test(DPH_DIC,N,Int1,"dp_hash");
  177.   newline;
  178.   dic_test(SK_DIC,N,Int1,"skiplist");
  179.   dic_test(RS_DIC,N,Int1,"rs_tree");
  180.   //dic_test(BIN_DIC,N,Int1,"bin_tree");
  181.   dic_test(RB_DIC,N,Int1,"rb_tree");
  182.   dic_test(AVL_DIC,N,Int1,"avl_tree");
  183.   dic_test(BB_DIC,N,Int1,"bb_tree");
  184.   dic_test(AB_DIC,N,Int1,"ab_tree");
  185.   newline;
  186.  
  187.  
  188.   dic_test(RB_DIC1,N,Float,"rb_tree");
  189.   dic_test(AVL_DIC1,N,Float,"avl_tree");
  190.   dic_test(SK_DIC1,N,Float,"skiplist");
  191.   dic_test(RS_DIC1,N,Float,"rs_tree");
  192.   dic_test(BIN_DIC1,N,Float,"bin_tree");
  193.   dic_test(BB_DIC1,N,Float,"bb_tree");
  194.   dic_test(AB_DIC1,N,Float,"ab_tree");
  195.   newline;
  196.  
  197.   return 0;
  198. }
  199.  
  200.  
  201.